'use strict';
/**
 * @fileoverview 抽奖
 * @author sundbcn@gmail.com (sun debing)
 */

var mongoose = require('mongoose');
var moment = require('moment');
var _ = require('lodash');
var luckyMgr = require('../mgr/luckyMgr');
var constantUtil = require('../util/constantUtil.js');
var turntableConf = require('../config/turntableConf.js');
var ObjectId = mongoose.Types.ObjectId;

///////////////////////////////////////////////////////////
/**
 * 转盘奖励item
 * @type {mongoose}
 */
var turntableItemSchema = new mongoose.Schema({

    hole_num: {
        type: Number,
        default: 0
    },

    id: {
        type: Number,
        default: 0
    },

    reward: {
        type: Number,
        default: 0
    },

    reward_num: {
        type: Number,
        default: 0
    },

    reward_type: {
        type: Number,
        default: 0
    },

    reward_value: {
        type: Number,
        default: 0
    },

    // 位置
    reward_position: {
        type: Number,
        default: 0
    },

    // 领取状态
    reward_state: {
        type: Number,
        default: 0
    }
});

var luckyItemSchema = new mongoose.Schema({
    new_id: {
        type: Number,
        default: 0
    }
});

/**
 * 设置领取状态
 * @return {[type]} [description]
 */
turntableItemSchema.methods.setRewardState = function () {
    this.reward_state = 1;
};

if (!turntableItemSchema.options.toJSON) {
    turntableItemSchema.options.toJSON = {};
}
/* jshint unused:false */
turntableItemSchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
    delete ret.__v;
};

///////////////////////////////////////////////////////////
/**
 *  初始化每日免费次数
 */

var _glodFreeNum = function () {
    return constantUtil.value('gold_lottery_free_times');
};

var _freeTurntableTime = function () {
    return moment().unix();
};

var _initRewardList = function () {
    return turntableConf.getConf();
};

var _initReceivedList = function () {
    var list = [];
    for (var i = 1; i <= 8; i++) {
        list.push({
            new_id: i,
            _id: new ObjectId()
        });
    }
    return list;
};
//------------------------------------------------------------------------------
/**
 * 抽奖
 */
var luckySchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 领取奖励次数
    receive_num: {
        type: Number,
        default: 1
    },

    // 转盘免费重置次数
    turntable_num: {
        type: Number,
        default: 1
    },

    // 免费重置次数时间
    free_turntable_time: {
        type: Number,
        default: _freeTurntableTime
    },

    // 转盘未领取位置列表
    receive_turntable_list: {
        type: [luckyItemSchema],
        default: _initReceivedList
    },

    // 转盘奖励列表
    turntable_list: {
        type: [turntableItemSchema],
        default: _initRewardList
    },

    // 新手材料转盘领取状态
    guide_state: {
        type: Number,
        default: 0
    }
});

luckySchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 新手引导领取状态
 * @return {[type]} [description]
 */
luckySchema.methods.getGuideState = function () {
    return this.guide_state;
};

luckySchema.methods.setGuideState = function () {
    this.guide_state = 1;
};

/**
 * 转盘当前是否有可以转动的奖励
 * @return {[type]} [description]
 */
luckySchema.methods.isStart = function () {
    var list = this.turntable_list;
    var isVail = false;
    _.some(list, function (item) {
        if (!item.reward_state) {
            isVail = true;
            return true;
        }
    });
    return isVail;
};

/**
 * 重置
 * @return {[type]} [description]
 */
luckySchema.methods.reset = function () {
    this.receive_turntable_list = _initReceivedList();
    this.turntable_list = _initRewardList();
    this.receive_num = 1;
};

/**
 * 转盘当前可领取的奖励    已领取
 * @return {[type]} [description]
 */
luckySchema.methods.removeId = function (id) {
    _.some(this.receive_turntable_list, function (key) {
        if (key.new_id == id) {
            key.remove();
            return true;
        }
    });
};

/**
 * 转盘当前未领取的奖励位置
 * @return {[type]} [description]
 */
luckySchema.methods.getReceiveList = function () {
    return this.receive_turntable_list;
};

/**
 * 增加领取奖励次数
 * @return {[type]} [description]
 */
luckySchema.methods.addNum = function () {
    if (this.receive_num < 9) {
        this.receive_num++;
        return true;
    }
    return false;
};

luckySchema.methods.addReceiveNum = function () {
    this.receive_num++;
};

/**
 * 获取领取奖励次数
 * @return {[type]} [description]
 */
luckySchema.methods.getNum = function () {
    return this.receive_num;
};

luckySchema.methods.setNum = function () {
    this.receive_num = 1;
};

/**
 * 获取转盘次数
 * @return {[type]} [description]
 */
luckySchema.methods.getTurntableNum = function () {
    return this.turntable_num;
};

/**
 * 获取转盘次数
 * @return {[type]} [description]
 */
luckySchema.methods.subTurntableNum = function () {
    if (this.turntable_num > 0) {
        this.turntable_num--;
    }
};

/**
 * 获取当前奖励列表
 * @return {[type]} [description]
 */
luckySchema.methods.getNowRewardList = function () {
    return this.turntable_list;
};

/**
 * 设置领取状态
 * @return {[type]} [description]
 */
luckySchema.methods.setRewardList = function (reward_position) {
    _.some(this.turntable_list, function (value) {
        if (value.reward_position == reward_position) {
            value.setRewardState();
            return true;
        }
    });
};

/**
 * 根据位置获取奖励信息
 * @return {[type]} [description]
 */
luckySchema.methods.getRewardList = function (reward_position) {
    var item = {};
    var isVail = false;
    _.some(this.turntable_list, function (value) {
        if (value.reward_position == reward_position) {
            item = value;
            isVail = true;
            return true;
        }
    });
    if (!!isVail) {
        return item;
    } else {
        return null;
    }
};

/**
 * 获取时间
 * @return {[type]} [description]
 */
luckySchema.methods.getTime = function () {
    return this.free_turntable_time;
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
luckySchema.methods.getLastChange = function () {
    return this.toJSON();
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
luckySchema.methods.push = function () {
    this.pushModify();
};

luckySchema.methods.pushModify = function () {
    luckyMgr.sendLuckyChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send luck message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

if (!luckySchema.options.toJSON) {
    luckySchema.options.toJSON = {};
}
/* jshint unused:false */
luckySchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Lucky', luckySchema);
